package com.wmx.newspushmonitor.notification;

import android.app.Notification;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.v4.content.LocalBroadcastManager;

import com.rhino.ui.utils.LogUtils;
import com.wmx.newspushmonitor.MyPushReceiver;

/**
 * @author LuoLin
 * @since Create on 2019/2/22.
 */
public class MyNotificationListenerService extends NotificationListenerService {
    private static final String TAG = "PushMonitorService";
    public static final String PKG_QQ = "com.tencent.mobileqq";
    public static final String PKG_WX = "com.tencent.mm";
    public static final String PKG_ZFB = "com.eg.android.AlipayGphone";
    public static final String PKG_DX = "com.android.mms";

    public static final String[] PKG_ARRAY = {
            PKG_QQ, PKG_WX, PKG_ZFB, PKG_DX
    };

    public static void startService(Context context) {
        context.startService(new Intent(context, MyNotificationListenerService.class));
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.d("开启服务：MyNotificationListenerService");
//        startForeground(1,getNotification());
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        LogUtils.d("通知所属包名：" + sbn.getPackageName());
        Notification notification = sbn.getNotification();
        if (notification == null) {
            LogUtils.d("notification is null");
            return;
        }
        Bundle extras = notification.extras;
        if (extras != null) {
            String title = extras.getString(Notification.EXTRA_TITLE, "");
            LogUtils.d("通知的标题为：" + title);
            String content = extras.getString(Notification.EXTRA_TEXT, "");
            LogUtils.d("通知的内容为：" + content);

            if (!title.isEmpty() && !content.isEmpty() && isCare(sbn.getPackageName())) {
                MyPushReceiver.sendMsg(getApplicationContext(), sbn.getPackageName(), title, content);
            }
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        LogUtils.d("监听到包：" + sbn.getPackageName() + " 的通知被移除");
        Intent localIntent = new Intent(Constants.BROADCAST_NOTIFICATION_REMOVED_ACTION);
        localIntent.putExtra("packageName", sbn.getPackageName());
        LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
    }

    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.d("监听服务被销毁");
//        stopForeground(true);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

    @Override
    public void onListenerDisconnected() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            requestRebind(new ComponentName(this, MyNotificationListenerService.class));
        }
    }

    /**
     * 是否关心
     */
    public static boolean isCare(String packgeName) {
        for (String n : PKG_ARRAY) {
            if (n.equals(packgeName)) {
                return true;
            }
        }
        return false;
    }
}
